__author__ = 'Soul'

from DbOperator import *
import json

class QixinDbOperator(DbOperator):
    def __init__(self, conn):
        DbOperator.__init__(self, conn)
        self.__com_basic_info_t = 'com_basic_info_t'
        self.__com_job_info_t = 'com_job_info_t'
        self.__com_ability_info_t = 'com_ability_info_t'
        self.__com_investment_info_t = 'com_investment_info_t'

    def __del__(self):
        self.close()

    def insert_basic_info(self, basic_info):
        if basic_info:
            com_basic_info = basic_info['com_basic_info']
            col_keys = com_basic_info.keys()
            col_keys.extend(['com_shareholders', 'com_members'])
            str_colns = ['%s'] * len(col_keys)
            columns = ','.join(col_keys)
            strcolns = ','.join(str_colns)

            com_shareholders = basic_info['com_shareholders']
            str_com_shdrs = json.dumps(com_shareholders)
            com_members = basic_info['com_members']
            str_com_mems = json.dumps(com_members)

            sql = 'insert into %s(%s) value (%s)' %(
                self.__com_basic_info_t,
                columns,
                strcolns)

            tvalues = com_basic_info.values()
            params = tvalues
            params.extend([str_com_shdrs, str_com_mems])
            self.__do_insert__(sql, params)

    def insert_job_info(self, qixin_code, job_info_list):
        if job_info_list and qixin_code:
            sql = 'insert into %s'%(self.__com_job_info_t) +\
                  '(com_qixin_code,position,qualification,source,size,url,date,location,years,title,salary,education,description) values(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)'

            items = []
            for job_info in job_info_list:
                items.append([
                    qixin_code,
                    job_info['position'],
                    job_info['qualification'],
                    job_info['source'],
                    job_info['size'],
                    job_info['url'],
                    job_info['date'],
                    job_info['location'],
                    job_info['years'],
                    job_info['title'],
                    job_info['salary'],
                    job_info['education'],
                    job_info['description']
                ])

            self.__do_insert_many__(sql, items)

    def insert_ability_info(self, qixin_code, ability_info_list):
        if ability_info_list and qixin_code:
            sql = 'insert into %s'%(self.__com_ability_info_t) +\
                  '(com_qixin_code,_id,patent_name,type_name,outhor_date,request_num,request_date,category_num,brief) values(%s, %s, %s, %s, %s, %s, %s, %s, %s)'

            items = []
            for ability_info in ability_info_list:
                items.append([
                    qixin_code,
                    ability_info['_id'],
                    ability_info['patent_name'],
                    ability_info['type_name'],
                    ability_info['outhor_date'],
                    ability_info['request_num'],
                    ability_info['request_date'],
                    ability_info['category_num'],
                    ability_info['brief'],
                ])
            self.__do_insert_many__(sql, items)

    def insert_investment_info(self, qixin_code, investment_info_list):
        if investment_info_list and qixin_code:
            sql = 'insert into %s'%(self.__com_investment_info_t) +\
                  '(com_qixin_code,id,name,oper_name,start_year,reg_capi,domain,status,logo_url) value(%s, %s, %s, %s, %s, %s, %s, %s, %s)'

            items = []
            for investment_info in investment_info_list:
                items.append([
                    qixin_code,
                    investment_info['id'],
                    investment_info['name'],
                    investment_info['oper_name'],
                    investment_info['start_year'],
                    investment_info['reg_capi'],
                    investment_info['domain'],
                    investment_info['status'],
                    investment_info['logo_url'],
                ])

            self.__do_insert_many__(sql, items)
